<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "variable".
 *
 * @property integer $vid
 * @property string $name
 * @property string $value
 */
class Variable extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'variable';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['value'], 'string'],
            [['name'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'vid' => 'pk',
            'name' => '变量名',
            'value' => '变量值',
        ];
    }

    /**
     * 方法说明：通过name取value，转换为对应格式，默认json
     *
     * 作者：张华杰 blues@lonlife.cn
     * 时间：2017-10-28 3:00 PM
     */
    public function variableGet($name, $format = 'json'){
        $result = self::findOne("name = $name");
        if ($result) {
            // return $result->attributes;
            $value = $result->attributes;
            $jsonData = $value['value'];
            if ($format == 'serialize') {
                $arrData = unserialize($jsonData);
            } else {
                $arrData = json_decode($jsonData, TRUE);
            }
            return $arrData;
        }
        return array();
    }

}
